package com.ouyy.volunteerservice.volunteer.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ouyy.volunteerservice.volunteer.domain.po.Evaluation;
import com.ouyy.volunteerservice.volunteer.service.EvaluationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 评价管理 Controller（使用 MyBatis-Plus）
 */
@RestController
@RequestMapping("/evaluation")
public class EvaluationController {

    @Autowired
    private EvaluationService evaluationService;

    /**
     * 获取所有评价列表
     */
    @GetMapping("/list")
    public List<Evaluation> getAllEvaluations() {
        return evaluationService.list();
    }

    /**
     * 根据 ID 获取评价详情
     */
    @GetMapping("/{id}")
    public Evaluation getEvaluationById(@PathVariable Integer id) {
        return evaluationService.getById(id);
    }

    /**
     * 添加新的评价
     */
    @PostMapping("/add")
    public boolean addEvaluation(@RequestBody Evaluation evaluation) {
        return evaluationService.save(evaluation);
    }

    /**
     * 更新评价
     */
    @PutMapping("/update")
    public boolean updateEvaluation(@RequestBody Evaluation evaluation) {
        Integer organizationId = evaluation.getOrganizationId();
        Integer volunteerId = evaluation.getVolunteerId();
        if (evaluationService.updateScoreByOrganizationIdAndVolunteerId(organizationId.toString(), volunteerId.toString(), evaluation.getScore())) {
            return true;
        } else {
           return false;
        }

    }

    /**
     * 根据 ID 删除评价
     */
    @DeleteMapping("/delete/{id}")
    public boolean deleteEvaluation(@PathVariable Integer id) {
        return evaluationService.removeById(id);
    }

    /**
     * 根据组织 ID 获取评价列表
     */
    @GetMapping("/byOrganization/{organizationId}")
    public List<Evaluation> getEvaluationsByOrganization(@PathVariable Integer organizationId) {
        return evaluationService.list(new QueryWrapper<Evaluation>().eq("organization_id", organizationId));
    }

    /**
     * 分页获取评价列表
     */
    @GetMapping("/page")
    public Page<Evaluation> getEvaluationPage(@RequestParam(defaultValue = "1") int pageNum,
                                              @RequestParam(defaultValue = "10") int pageSize) {
        return evaluationService.page(new Page<>(pageNum, pageSize));
    }


    @GetMapping("/scores/{organizationId}/{volunteerId}")
    public String getScoresByOrganizationAndVolunteer(
            @PathVariable String organizationId,
            @PathVariable String volunteerId) {
        return evaluationService.queryByorganationId(organizationId, volunteerId);
    }


}
